home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / deque.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  27.0 KB  |  830 lines

  1. #ifndef __STD_DEQUE__
  2. #define __STD_DEQUE__
  3. #pragma option push -b -a4 -Vx- -Ve- -w-inl -w-aus -w-sig
  4.  
  5. /***************************************************************************
  6.  *
  7.  * deque - Declaration and definition for the Standard Library deque class
  8.  *
  9.  * $Id: deque,v 1.60 1996/08/28 18:41:26 smithey Exp $
  10.  *
  11.  ***************************************************************************
  12.  *
  13.  * Copyright (c) 1994
  14.  * Hewlett-Packard Company
  15.  *
  16.  * Permission to use, copy, modify, distribute and sell this software
  17.  * and its documentation for any purpose is hereby granted without fee,
  18.  * provided that the above copyright notice appear in all copies and
  19.  * that both that copyright notice and this permission notice appear
  20.  * in supporting documentation.  Hewlett-Packard Company makes no
  21.  * representations about the suitability of this software for any
  22.  * purpose.  It is provided "as is" without express or implied warranty.
  23.  *
  24.  *
  25.  ***************************************************************************
  26.  *
  27.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  28.  * ALL RIGHTS RESERVED *
  29.  * The software and information contained herein are proprietary to, and
  30.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  31.  * intends to preserve as trade secrets such software and information.
  32.  * This software is furnished pursuant to a written license agreement and
  33.  * may be used, copied, transmitted, and stored only in accordance with
  34.  * the terms of such license and with the inclusion of the above copyright
  35.  * notice.  This software and information or any other copies thereof may
  36.  * not be provided or otherwise made available to any other person.
  37.  *
  38.  * Notwithstanding any other lease or license that may pertain to, or
  39.  * accompany the delivery of, this computer software and information, the
  40.  * rights of the Government regarding its use, reproduction and disclosure
  41.  * are as set forth in Section 52.227-19 of the FARS Computer
  42.  * Software-Restricted Rights clause.
  43.  * 
  44.  * Use, duplication, or disclosure by the Government is subject to
  45.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  46.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  47.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  48.  * P.O. Box 2328, Corvallis, Oregon 97339.
  49.  *
  50.  * This computer software and information is distributed with "restricted
  51.  * rights."  Use, duplication or disclosure is subject to restrictions as
  52.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  53.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  54.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  55.  * then the "Alternate III" clause applies.
  56.  *
  57.  **************************************************************************/
  58.  
  59. #include "rw/stddefs.h"
  60.  
  61. #include <stdcomp.h>
  62. #ifndef _RWSTD_HEADER_REQUIRES_HPP
  63. #include <algorithm>
  64. #include <iterator>
  65. #include <memory>
  66. #include <stdexcept>
  67. #else
  68. #include <algorithm.hpp>
  69. #include <iterator.hpp>
  70. #include <memory.hpp>
  71. #include <stdexcept.hpp>
  72. #endif
  73.  
  74. #ifndef deque 
  75. #define deque deque
  76. #endif
  77.  
  78. #ifndef _RWSTD_NO_NAMESPACE
  79. namespace std {
  80. #endif
  81.  
  82. //
  83. // Note that _RWSTD_SIMPLE_DEFAULT(x)
  84. // will expand to: ' = x', or nothing,
  85. // depending on your compiler's capabilities and/or
  86. // flag settings (see stdcomp.h).
  87. //
  88. template <class T, class Allocator _RWSTD_SIMPLE_DEFAULT(allocator<T>) >
  89. class  deque
  90. {
  91.   protected:
  92. #ifdef _RWSTD_ALLOCATOR
  93.     typedef _TYPENAME Allocator::rebind<T>::other     value_alloc_type;
  94. #else
  95.     typedef allocator_interface<Allocator,T>    value_alloc_type;
  96. #endif
  97.  
  98.   public:
  99.  
  100.     class  iterator;
  101.     class  const_iterator;
  102.     friend class iterator;
  103.     friend class const_iterator;
  104.  
  105.   public:
  106.     //
  107.     // Types.
  108.     //
  109.     typedef T                                          value_type;
  110.     typedef Allocator                                  allocator_type;
  111.  
  112. #ifndef _RWSTD_NO_COMPLICATED_TYPEDEF
  113.     typedef _TYPENAME _RWSTD_ALLOC_SIZE_TYPE            size_type;
  114.     typedef _TYPENAME _RWSTD_ALLOC_DIFF_TYPE            difference_type;
  115.     typedef _TYPENAME value_alloc_type::reference       reference;
  116.     typedef _TYPENAME value_alloc_type::const_reference const_reference;
  117.   protected:
  118.     typedef _TYPENAME value_alloc_type::pointer       pointer;
  119.     typedef _TYPENAME value_alloc_type::const_pointer const_pointer;
  120. #else
  121.     typedef size_t            size_type;
  122.     typedef ptrdiff_t         difference_type;
  123.     typedef T&                reference;
  124.     typedef const T&          const_reference;
  125.   protected:
  126.     typedef T*                pointer;
  127.     typedef const T*          const_pointer;
  128. #endif  //_RWSTD_NO_COMPLICATED_TYPEDEF
  129.  
  130. #ifdef _RWSTD_ALLOCATOR
  131.     typedef _TYPENAME Allocator::rebind<pointer>::other  map_alloc_type;
  132. #else
  133.     typedef allocator_interface<allocator_type,pointer> map_alloc_type;
  134. #endif
  135.     typedef _TYPENAME map_alloc_type::pointer            map_pointer;
  136.  
  137.     allocator_type          the_allocator;
  138.  
  139.     static size_type buffer_size ();
  140.  
  141.   public:
  142.     //
  143.     // Definition of our iterator.
  144.     //
  145.     class iterator : public random_access_iterator<T, difference_type>
  146.     {
  147.         friend class deque<T,Allocator>;
  148.         friend class const_iterator;
  149.  
  150.       protected:
  151.  
  152.         pointer     current;
  153.         pointer     first;
  154.         pointer     last;
  155.         map_pointer node;
  156.  
  157.         iterator (pointer x, map_pointer y)
  158.             : current(x), first(*y), last(*y + buffer_size()), node(y) {}
  159.  
  160.       public:
  161.  
  162.         iterator () : current(0), first(0), last(0), node(0) {}
  163.         reference operator* () const { return *current; }
  164.         difference_type operator- (const iterator& x) const
  165.         {
  166.             return node == x.node 
  167.                 ? current - x.current 
  168.                 : difference_type(buffer_size() * (node - x.node - 1) +
  169.                                   (current - first) + (x.last - x.current));
  170.         }
  171.         iterator& operator++ ()
  172.         {
  173.             if (++current == last)
  174.             {
  175.                 first = *(++node);
  176.                 current = first;
  177.                 last = first + buffer_size();
  178.             }
  179.             return *this; 
  180.         }
  181.         iterator operator++ (int)
  182.         {
  183.             iterator tmp = *this; ++*this; return tmp;
  184.         }
  185.         iterator& operator-- ()
  186.         {
  187.             if (current == first)
  188.             {
  189.                 first = *(--node);
  190.                 last = first + buffer_size();
  191.                 current = last;
  192.             }
  193.             --current;
  194.             return *this;
  195.         }
  196.         iterator operator-- (int)
  197.         {
  198.             iterator tmp = *this; --*this; return tmp;
  199.         }
  200.         iterator& operator+= (difference_type n)
  201.         {
  202.             difference_type offset = n + (current - first);
  203.             difference_type num_node_to_jump = offset >= 0
  204.                 ? offset / buffer_size()
  205.                 : -(difference_type)((-offset + buffer_size() - 1) / buffer_size());
  206.             if (num_node_to_jump == 0)
  207.                 current += n;
  208.             else
  209.             {
  210.                 node = node + num_node_to_jump;
  211.                 first = *node;
  212.                 last = first + buffer_size();
  213.                 current = first + (offset - num_node_to_jump * buffer_size());
  214.             }
  215.             return *this;
  216.         }
  217.         iterator& operator-= (difference_type n) { return *this += -n; }
  218.         iterator operator+ (difference_type n) const
  219.         {
  220.             iterator tmp = *this; return tmp += n;
  221.         }
  222.         iterator operator- (difference_type n) const
  223.         {
  224.             iterator tmp = *this; return tmp -= n;
  225.         }
  226.         reference operator[] (difference_type n) { return *(*this + n); }
  227.         bool operator== (const iterator& x) const
  228.         {
  229.             return current == x.current || 
  230.                 ((current == first || x.current == x.first) && 
  231.                  *this - x == 0);
  232.         }
  233.         bool operator!= (const iterator& x) const { return !(*this == x); } 
  234.         bool operator< (const iterator& x) const
  235.         {
  236.             return (node == x.node) ? (current < x.current) : (node < x.node);
  237.         } 
  238.         bool operator> (const iterator& x) const
  239.         {
  240.             return x < *this;
  241.         }
  242.         bool operator>= (const iterator& x) const
  243.         {
  244.             return !(*this < x);
  245.         }
  246.         bool operator<= (const iterator& x) const
  247.         {
  248.             return !(*this > x);
  249.         }
  250.     };  // End of nested definiton of iterator.
  251.  
  252.     //
  253.     // Definition of our constant iterator.
  254.     //
  255.     class const_iterator : public random_access_iterator<T, difference_type>
  256.     {
  257.         friend class deque<T,Allocator>;
  258.  
  259.       protected:
  260.  
  261.         pointer     current;
  262.         pointer     first;
  263.         pointer     last;
  264.         map_pointer node;
  265.  
  266.         const_iterator (pointer x, map_pointer y) 
  267.             : current(x), first(*y), last(*y + buffer_size()), node(y) {}
  268.  
  269.       public:
  270.        
  271.         const_iterator () : current(0), first(0), last(0), node(0) {}
  272.         const_iterator (const iterator& x) 
  273.             : current(x.current), first(x.first), last(x.last), node(x.node) {}     
  274.         const_reference operator* () const { return *current; }
  275.         difference_type operator- (const const_iterator& x) const
  276.         {
  277.             return node == x.node 
  278.             ? current - x.current 
  279.             : difference_type(buffer_size() * (node - x.node - 1) +
  280.                   (current - first) + (x.last - x.current));
  281.         }
  282.         const_iterator& operator++ ()
  283.         {
  284.             if (++current == last)
  285.             {
  286.                 first = *(++node);
  287.                 current = first;
  288.                 last = first + buffer_size();
  289.             }
  290.         return *this; 
  291.         }
  292.         const_iterator operator++ (int)
  293.         {
  294.             const_iterator tmp = *this; ++*this; return tmp;
  295.         }
  296.         const_iterator& operator-- ()
  297.         {
  298.             if (current == first)
  299.             {
  300.                 first = *(--node);
  301.                 last = first + buffer_size();
  302.                 current = last;
  303.             }
  304.             --current;
  305.             return *this;
  306.         }
  307.         const_iterator operator-- (int)
  308.         {
  309.             const_iterator tmp = *this; --*this; return tmp;
  310.         }
  311.         const_iterator& operator+= (difference_type n)
  312.         {
  313.             difference_type offset = n + (current - first);
  314.             difference_type num_node_to_jump = offset >= 0
  315.                 ? offset / buffer_size()
  316.                 : -(difference_type)((-offset + buffer_size() - 1) / buffer_size());
  317.             if (num_node_to_jump == 0)
  318.                 current += n;
  319.             else
  320.             {
  321.                 node = node + num_node_to_jump;
  322.                 first = *node;
  323.                 last = first + buffer_size();
  324.                 current = first + (offset - num_node_to_jump * buffer_size());
  325.             }
  326.             return *this;
  327.         }
  328.         const_iterator& operator-= (difference_type n) { return *this += -n; }
  329.         const_iterator operator+ (difference_type n) const
  330.         {
  331.             const_iterator tmp = *this; return tmp += n;
  332.         }
  333.         const_iterator operator- (difference_type n) const
  334.         {
  335.             const_iterator tmp = *this; return tmp -= n;
  336.         }
  337.         const_reference operator[] (difference_type n)
  338.         { 
  339.             return *(*this + n); 
  340.         }
  341.         bool operator== (const const_iterator& x) const
  342.         {
  343.             return current == x.current || 
  344.                 ((current == first || x.current == x.first) && 
  345.              *this - x == 0);
  346.         }
  347.         bool operator!= (const const_iterator& x) const { return !(*this == x); }
  348.         bool operator< (const const_iterator& x) const
  349.         {
  350.             return (node == x.node) ? (current < x.current) : (node < x.node);
  351.         } 
  352.         bool operator> (const const_iterator& x) const
  353.         {
  354.             return x < *this;
  355.         }
  356.         bool operator>= (const const_iterator& x) const
  357.         {
  358.             return !(*this < x);
  359.         }
  360.         bool operator<= (const const_iterator& x) const
  361.         {
  362.             return !(*this > x);
  363.         }
  364.     };  // End of nested definiton of const_iterator.
  365.  
  366.     typedef _STD::reverse_iterator<const_iterator, value_type, 
  367.             const_reference, const_pointer, difference_type>
  368.             const_reverse_iterator;
  369.     typedef _STD::reverse_iterator<iterator, value_type, reference, 
  370.                              pointer, difference_type> 
  371.             reverse_iterator;
  372.  
  373.   protected:
  374.  
  375.     iterator    start;
  376.     iterator    finish;
  377.     size_type   length;
  378.     map_pointer map;
  379.     size_type   map_size;
  380.  
  381.     void allocate_at_begin   ();
  382.     void allocate_at_end     ();
  383.     void deallocate_at_begin ();
  384.     void deallocate_at_end   ();
  385.     void insert_aux (iterator position, size_type n, const T& x);
  386.  
  387.   public:
  388.     //
  389.     // construct/copy/destroy
  390.     //
  391.     _EXPLICIT deque (const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator())) 
  392.       : start(), finish(), length(0), map(0), map_size(0),
  393.         the_allocator(alloc) 
  394.     { ; }
  395.  
  396. #ifdef _RWSTD_NO_DEFAULT_TEMPLATE_ARGS
  397.     deque (void) 
  398.       : start(), finish(), length(0), map(0), map_size(0),
  399.         the_allocator(Allocator()) 
  400.     { ; }
  401. #endif
  402.  
  403.     //
  404.     // Build a deque of size n with each element set to default for type T.
  405.     // Requires that T have a default constructor.
  406.     //
  407.     _EXPLICIT deque (size_type n, const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  408.         : start(), finish(), length(0), map(0), map_size(0),
  409.         the_allocator(alloc) 
  410.     {
  411.         insert(begin(), n, T());
  412.     }
  413.  
  414. #ifdef _RWSTD_NO_DEFAULT_TEMPLATE_ARGS
  415.     _EXPLICIT deque (size_type n)
  416.         : start(), finish(), length(0), map(0), map_size(0),
  417.         the_allocator(Allocator()) 
  418.     {
  419.         insert(begin(), n, T());
  420.     }
  421.  
  422.     deque (size_type n, const T& value)
  423.         : start(), finish(), length(0), map(0), map_size(0),
  424.         the_allocator(Allocator()) 
  425.     {
  426.         insert(begin(), n, value);
  427.     }
  428. #endif
  429.  
  430.     deque (const deque<T,Allocator>& x)
  431.         : start(), finish(), length(0), map(0), map_size(0)
  432.     {
  433.         the_allocator = x.get_allocator();
  434.         copy(x.begin(), x.end(), back_inserter(*this));
  435.     }
  436.  
  437. #ifndef _RWSTD_NO_MEMBER_TEMPLATES
  438.     template<class InputIterator>
  439.     deque (InputIterator first, InputIterator last, const Allocator& alloc = Allocator())
  440.         : start(), finish(), length(0), map(0), map_size(0),
  441.         the_allocator(alloc) 
  442.     {
  443.         copy(first, last, back_inserter(*this));
  444.     }
  445.     deque (int n, const T& value,
  446.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  447.           : the_allocator(alloc)
  448.     { insert(begin(), (size_type)n, value); }
  449.     deque (unsigned int n, const T& value,
  450.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  451.           : the_allocator(alloc)
  452.     { insert(begin(), (size_type)n, value); }
  453.     deque (long n, const T& value,
  454.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  455.           : the_allocator(alloc)
  456.     { insert(begin(), (size_type)n, value); }
  457.     deque (unsigned long n, const T& value,
  458.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  459.           : the_allocator(alloc)
  460.     { insert(begin(), (size_type)n, value); }
  461.     deque (short n, const T& value,
  462.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  463.           : the_allocator(alloc)
  464.     { insert(begin(), (size_type)n, value); }
  465.     deque (unsigned short n, const T& value,
  466.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  467.           : the_allocator(alloc)
  468.     { insert(begin(), (size_type)n, value); }
  469.     deque (char n, const T& value,
  470.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  471.           : the_allocator(alloc)
  472.     { insert(begin(), (size_type)n, value); }
  473.     deque (unsigned char n, const T& value,
  474.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  475.           : the_allocator(alloc)
  476.     { insert(begin(), (size_type)n, value); }
  477.     deque (bool n, const T& value,
  478.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  479.           : the_allocator(alloc)
  480.     {  insert(begin(), (size_type)n, value); }
  481. #ifndef _RWSTD_NO_OVERLOAD_WCHAR
  482.     deque (wchar_t n, const T& value,
  483.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  484.           : the_allocator(alloc)
  485.     {  insert(begin(), (size_type)n, value); }
  486. #endif
  487. #else
  488.     //
  489.     // Build a deque of size n with each element set to copy of value.
  490.     //
  491.     deque (size_type n, const T& value, const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  492.         : start(), finish(), length(0), map(0), map_size(0),
  493.         the_allocator(alloc) 
  494.     {
  495.         insert(begin(), n, value);
  496.     }
  497.  
  498.  
  499.     deque (const_iterator first, const_iterator last, const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  500.         : start(), finish(), length(0), map(0), map_size(0),
  501.         the_allocator(alloc) 
  502.     {
  503.         copy(first, last, back_inserter(*this));
  504.     }
  505.     
  506.     deque (const T* first, const T* last, const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  507.         : start(), finish(), length(0), map(0), map_size(0),
  508.         the_allocator(alloc) 
  509.     {
  510.         copy(first, last, back_inserter(*this));
  511.     }
  512.  
  513. #ifdef _RWSTD_NO_DEFAULT_TEMPLATE_ARGS
  514.     deque (const_iterator first, const_iterator last)
  515.         : start(), finish(), length(0), map(0), map_size(0),
  516.         the_allocator(Allocator()) 
  517.     {
  518.         copy(first, last, back_inserter(*this));
  519.     }
  520.     
  521.     deque (const T* first, const T* last)
  522.         : start(), finish(), length(0), map(0), map_size(0),
  523.         the_allocator(Allocator()) 
  524.     {
  525.         copy(first, last, back_inserter(*this));
  526.     }
  527. #endif
  528. #endif
  529.  
  530.     ~deque ();
  531.     deque<T,Allocator>& operator= (const deque<T,Allocator>& x)
  532.     {
  533.         if (!(this == &x))
  534.         {
  535.             if (size() >= x.size()) 
  536.                 erase(copy(x.begin(), x.end(), begin()), end());
  537.             else 
  538.                 copy(x.begin() + size(), x.end(),
  539.                      inserter(*this,copy(x.begin(),x.begin()+size(),begin())));
  540.         }
  541.         return *this;
  542.     }
  543.  
  544. #ifndef _RWSTD_NO_MEMBER_TEMPLATES
  545.     template<class InputIterator>
  546.     void assign (InputIterator first, InputIterator last)
  547.     {
  548.         erase(begin(), end()); insert(begin(), first, last);
  549.     }
  550.     //
  551.     // Assign n copies of default value of type T to vector.
  552.     // This requires that T have a default constructor.
  553.     //
  554.     template<class Size>
  555.     void assign (Size n)
  556.     {
  557.         erase(begin(), end()); insert(begin(), n, T());
  558.     }
  559.     //
  560.     // Assign n copies of t to this vector.
  561.     //
  562.     template<class Size, class U>
  563.     void assign (Size n, const U& t)
  564.     {
  565.         erase(begin(), end()); insert(begin(), n, t);
  566.     }
  567. #else
  568.     void assign (const_iterator first, const_iterator last)
  569.     {
  570.         erase(begin(), end()); insert(begin(), first, last);
  571.     }
  572.     void assign (const T* first, const T* last)
  573.     {
  574.         erase(begin(), end()); insert(begin(), first, last);
  575.     }
  576.     //
  577.     // Assign n copies of default value of type T to vector.
  578.     // This requires that T have a default constructor.
  579.     //
  580.     void assign (size_type n)
  581.     {
  582.         erase(begin(), end()); insert(begin(), n, T());
  583.     }
  584.     //
  585.     // Assign n copies of t to this vector.
  586.     //
  587.     void assign (size_type n, const T& t)
  588.     {
  589.         erase(begin(), end()); insert(begin(), n, t);
  590.     }
  591. #endif
  592.     allocator_type get_allocator() const
  593.     {
  594.         return the_allocator;
  595.     }
  596.  
  597.     //
  598.     // Iterators.
  599.     //
  600.     iterator         begin  ()       { return start;  }
  601.     const_iterator   begin  () const { return start;  }
  602.     iterator         end    ()       { return finish; }
  603.     const_iterator   end    () const { return finish; }
  604.     reverse_iterator rbegin ()
  605.     { 
  606.       reverse_iterator tmp(end()); return tmp;
  607.     }
  608.     const_reverse_iterator rbegin () const
  609.     { 
  610.         const_reverse_iterator tmp(end()); return tmp;
  611.     }
  612.     reverse_iterator rend ()
  613.     { 
  614.         reverse_iterator tmp(begin()); return tmp;
  615.     }
  616.     const_reverse_iterator rend () const
  617.     { 
  618.         const_reverse_iterator tmp(begin()); return tmp;
  619.     }
  620.  
  621.     //
  622.     // Capacity.
  623.     //
  624.     size_type size     () const { return length;                    }
  625.     size_type max_size () const 
  626.     { return value_alloc_type(the_allocator).max_size(); }
  627.     bool      empty    () const { return length == 0;               }
  628.     void      resize (size_type new_size);
  629.     void      resize (size_type new_size, T value);
  630.  
  631.     //
  632.     // Element access.
  633.     //
  634.     reference       operator[] (size_type n)
  635.     {
  636. #ifdef _RWSTD_BOUNDS_CHECKING
  637.       _RWSTD_THROW(n >= size(), out_of_range, __RWSTD::rwse_OutOfRange);
  638.       return *(begin() + n);
  639. #else
  640.       return *(begin() + n);
  641. #endif
  642.     }
  643.     const_reference operator[] (size_type n) const
  644.     {
  645. #ifdef _RWSTD_BOUNDS_CHECKING
  646.       _RWSTD_THROW(n >= size(), out_of_range, __RWSTD::rwse_OutOfRange);
  647.       return *(begin() + n);
  648. #else
  649.       return *(begin() + n);
  650. #endif
  651.     }
  652.     const_reference at (size_type n)         const 
  653.     { 
  654.       _RWSTD_THROW(n >= size(), out_of_range, __RWSTD::rwse_OutOfRange);
  655.       return *(begin() + n); 
  656.     }
  657.     reference       at (size_type n)
  658.     { 
  659.       _RWSTD_THROW(n >= size(), out_of_range, __RWSTD::rwse_OutOfRange);
  660.       return *(begin() + n); 
  661.     }
  662.     reference       front ()                       { return *begin();       }
  663.     const_reference front ()                 const { return *begin();       }
  664.     reference       back ()                        { return *(end() - 1);   }
  665.     const_reference back ()                  const { return *(end() - 1);   }
  666.  
  667.     //
  668.     // Modifiers.
  669.     //
  670.     void push_front (const T& x)
  671.     {
  672.         if (empty() || begin().current == begin().first) allocate_at_begin();
  673.         --start.current;
  674.         value_alloc_type(the_allocator).construct(start.current, x);
  675.         ++length;
  676.     }
  677.     void push_back (const T& x)
  678.     {
  679.         if (empty() || end().current == end().last) allocate_at_end();
  680.         value_alloc_type(the_allocator).construct(finish.current, x);
  681.         ++finish.current;
  682.         ++length;
  683.     }
  684.     //
  685.     // Insert default value of type T at position.
  686.     // Requires that T have a default constructor.
  687.     //
  688.     iterator insert (iterator position);
  689.     //
  690.     // Insert x at position.
  691.     //
  692.     iterator insert (iterator position, const T& x);
  693.  
  694. #ifndef _RWSTD_NO_MEMBER_TEMPLATES
  695.     template <class InputIterator>
  696.     void insert(iterator position, InputIterator first, 
  697.                 InputIterator last);
  698.     void insert (iterator position, int n, const T& value)
  699.     { insert_aux(position,(size_type)n,value); }
  700.     void insert (iterator position, unsigned int n, const T& value)
  701.     { insert_aux(position,(size_type)n,value); }
  702.     void insert (iterator position, long n, T value)
  703.     { insert_aux(position,(size_type)n,value); }
  704.     void insert (iterator position, unsigned long n, const T& value)
  705.     { insert_aux(position,(size_type)n,value); }
  706.     void insert (iterator position, short n, const T& value)
  707.     { insert_aux(position,(size_type)n,value); }
  708.     void insert (iterator position, unsigned short n, const T& value)
  709.     { insert_aux(position,(size_type)n,value); }
  710.     void insert (iterator position, char n, const T& value)
  711.     { insert_aux(position,(size_type)n,value); }
  712.     void insert (iterator position, unsigned char n, const T& value)
  713.     { insert_aux(position,(size_type)n,value); }
  714.     void insert (iterator position, bool n, const T& value)
  715.     { insert_aux(position,(size_type)n,value); }
  716. #ifndef _RWSTD_NO_OVERLOAD_WCHAR
  717.     void insert (iterator position, wchar_t n, const T& value)
  718.     { insert_aux(position,(size_type)n,value); }
  719. #endif
  720. #else
  721.     void insert (iterator position, size_type n, const T& x)
  722.     { insert_aux(position,n,x); }
  723.     void insert (iterator position, const T* first, const T* last);
  724.     void insert (iterator position, const_iterator first, const_iterator last);
  725. #endif
  726.  
  727.     void pop_front ()
  728.     {
  729.         value_alloc_type(the_allocator).destroy(start.current);
  730.         ++start.current;
  731.         --length; 
  732.         if (empty() || begin().current == begin().last) deallocate_at_begin();
  733.     }
  734.     void pop_back ()
  735.     {
  736.         --finish.current;
  737.         value_alloc_type(the_allocator).destroy(finish.current);
  738.         --length; 
  739.         if (empty() || end().current == end().first) deallocate_at_end();
  740.     }
  741.     iterator erase (iterator position);
  742.     iterator erase (iterator first, iterator last);    
  743.     void clear()
  744.     {
  745.         erase(begin(),end());
  746.     }
  747.     void swap (deque<T,Allocator>& x)
  748.     {
  749.       if(the_allocator== x.the_allocator)
  750.        {
  751. #ifndef _RWSTD_NO_NAMESPACE
  752.         std::swap(start,          x.start);
  753.         std::swap(finish,         x.finish);
  754.         std::swap(length,         x.length);
  755.         std::swap(map,            x.map);
  756.         std::swap(map_size,       x.map_size);
  757.  #else
  758.         ::swap(start,             x.start);
  759.         ::swap(finish,            x.finish);
  760.         ::swap(length,            x.length);
  761.         ::swap(map,               x.map);
  762.         ::swap(map_size,          x.map_size);
  763.  #endif 
  764.        }
  765.        else
  766.        {
  767.          deque<T,Allocator> _x=*this;
  768.          *this=x;
  769.          x=_x;
  770.        }
  771.     }
  772. };
  773.  
  774. template <class T, class Allocator>
  775. inline bool operator== (const deque<T,Allocator>& x, const deque<T,Allocator>& y)
  776. {
  777.     return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
  778. }
  779.  
  780. template <class T, class Allocator>
  781. inline bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y)
  782. {
  783.     return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
  784. }
  785.  
  786. #if !defined(_RWSTD_NO_NAMESPACE) || !defined(_RWSTD_NO_PART_SPEC_OVERLOAD)
  787. template <class T, class Allocator>
  788. inline bool operator!= (const deque<T,Allocator>& x, const deque<T,Allocator>& y)
  789. {
  790.     return !(x == y);
  791. }
  792.  
  793. template <class T, class Allocator>
  794. inline bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y)
  795. {
  796.     return y < x;
  797. }
  798.  
  799. template <class T, class Allocator>
  800. inline bool operator>= (const deque<T,Allocator>& x, const deque<T,Allocator>& y)
  801. {
  802.     return !(x < y);
  803. }
  804.  
  805. template <class T, class Allocator>
  806. inline bool operator<= (const deque<T,Allocator>& x, const deque<T,Allocator>& y)
  807. {
  808.     return !(y <  x);
  809. }
  810.  
  811. template <class T, class Allocator>
  812. inline void swap(deque<T,Allocator>& a, deque<T,Allocator>& b)
  813. {
  814.     a.swap(b);
  815. }
  816. #endif
  817.  
  818. #ifndef _RWSTD_NO_NAMESPACE
  819. }
  820. #endif
  821.  
  822. #ifdef _RWSTD_NO_TEMPLATE_REPOSITORY
  823. #include <deque.cc>
  824. #endif
  825.  
  826. #undef deque
  827.  
  828. #pragma option pop
  829. #endif /*__STD_DEQUE__*/
  830.